package com.flying.client.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.CharBuffer;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.os.StatFs;

public class FileHelper {
	private static final Logger logger = Logger.getLogger(FileHelper.class.getName());
	public static final int MB = 1024 * 1024;
	/**
	 * 是否挂载了SD卡
	 */
	public static boolean hasExternalStroage()
	{
		String status = Environment.getExternalStorageState();
		if (status.equals(Environment.MEDIA_MOUNTED))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	/**
	 * 获取Sd卡上的空闲空间
	 */
	public static int getFreeSpaceInExternalStroage()
	{
		if (!hasExternalStroage())
			return 0;
		
		StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
		double sdFreeMB = ((double)stat.getAvailableBlocks() * (double) stat.getBlockSize()) / MB;
		return (int) sdFreeMB; 
	}
	/**
	 * 获取Sd卡的根目录
	 */
	public static String getExternalStoragePath()
	{
		return Environment.getExternalStorageDirectory().getPath();
	}
	/**
	 * 获取文件
	 * @param fileName
	 * @return
	 */
	public static File getFile(String fileName)
	{
		try
		{
			File file = new File(fileName);
			if (!file.exists())
				return null;
			
			return file;
		}
		catch(Exception e)
		{
			logger.debug("FileHelper.getFile Error="+e);
			return null;
		}
	}
	public static boolean createFile(String dictory,String fileName,byte[] byteBuffer)
	{
		try
		{
			if (byteBuffer == null)
				return false;
			
			createDictory(dictory);
			
			FileOutputStream os = new FileOutputStream(dictory + fileName);
			os.write(byteBuffer);
			os.close();
			
			return true;
		}
		catch(Exception e)
		{
			logger.debug("FileHelper.createFile Error="+e);
			return false;
		}
	}
	
	public static boolean deleteFile(String fileName)
	{
		try
		{
			File file = getFile(fileName);
			if (file == null)
				return true;
			
			return file.delete();
		}
		catch(Exception e)
		{
			logger.debug("FileHelper.deleteFile Error="+e);
			return false;
		}
	}
	
	public static Bitmap getFileToBitmap(String fileName)
	{
		try
		{
			File file = getFile(fileName);
			FileInputStream os = new FileInputStream(file);
			Bitmap bitmap = BitmapFactory.decodeStream(os);
			os.close();
			
			return bitmap;
			
		}
		catch(Exception e)
		{
			logger.debug("FileHelper.getFiletoBitmap Error="+e);
			return null;
		}
	}
	
	public static String getFileToString(String fileName)
	{
		String result = StringHelper.empty;
		try
		{
			File file = getFile(fileName);
			FileInputStream os = new FileInputStream(file);
			
			CharBuffer cb = null;
			try {
				cb = CharBuffer.allocate(os.available());
			} catch (IOException e1){
				
			}
			
			InputStreamReader reader = new InputStreamReader(os);
			
			if (cb != null)
			{
				reader.read(cb);
				result = new String(cb.array());
			}
			
			return result;	
		}
		catch(Exception e)
		{
			logger.debug("FileHelper.getFiletoBitmap Error="+e);
			return null;
		}
	}
	/**
	 * 创建目录
	 */
	public static boolean createDictory(String path)
	{
		try
		{
			File dictory = new File(path);
			if (!dictory.exists())
			{
				dictory.mkdir();
			}
			
			return true;
		}
		catch(Exception e)
		{
			logger.debug("FileHelper.createDictory Error="+e);
			return false;
		}
	}
	
	public static boolean deleteDictory(String path)
	{
		try
		{
			File dictory = new File(path);
			if (dictory.exists())
			{
				dictory.delete();
			}
			
			return true;
		}
		catch(Exception e)
		{
			logger.debug("FileHelper.deleteDictory Error="+e);
			return false;
		}
	}
}
